using System;
using System.Web.Mvc;
using JinTai.WebSite;
using JinTai.WebSite.Dao;
using Ornament.Web.Mvc;

namespace Ornament.MVCWebFrame.Controllers
{
    public class FaShiController : Controller
    {
        //
        // GET: /FaShi/

        public ActionResult Index(FormCollection collection)
        {
            var dao = new FashiScheduleDao();
            if (collection.HasKeys())
            {
                var id = new Guid(collection["showIt"]);

                var topFashi = dao.GetTopSchedule();
                if (topFashi != null)
                {
                    topFashi.Show = false;
                }
                dao.Get(id).Show = true;
            }
            ViewData["fashi"] = dao.FindAll(0, 10000, "CreateTime desc");
            return View();
        }

        public ActionResult List(FormCollection collection)
        {
            var dao = new FashiScheduleDao();
            if (collection.HasKeys())
            {
                var id = new Guid(collection["showIt"]);

                var topFashi = dao.GetTopSchedule();
                if (topFashi != null)
                {
                    topFashi.Show = false;
                }
                dao.Get(id).Show = true;
            }
            ViewData["fashi"] = dao.FindAll(0, 10000, "CreateTime desc");
            return View();
        }


        public ActionResult Edit(string id)
        {
            ViewData["id"] = id;
            return View();
        }

        public ActionResult Get(string id)
        {
            var dao = new FashiScheduleDao();
            var fashi = dao.Get(new Guid(id));
            return Json(fashi);
        }

        [JsonContainerFilter]
        public ActionResult Save(JsonContainer data)
        {
            try
            {
                var dao = new FashiScheduleDao();
                FashiSchedule fashi = String.IsNullOrEmpty(data.ToString("Id")) ? new FashiSchedule() : dao.Get(data.ToGuid("Id"));
                fashi.Subject = data.ToString("Subject");
                fashi.Items.Clear();
                int i = 0;
                foreach (var container in data.ToArray<JsonContainer>("Items"))
                {
                    var item = new FashiScheduleItem
                                   {
                                       Content = container.ToString("Content"),
                                       EndDate = container.ToDateTime("EndDate"),
                                       StartDate = container.ToDateTime("StartDate"),
                                       Memo = container.ToString("Memo"),
                                       Order = i,
                                       Subject = container.ToString("Subject")
                                   };
                    i++;
                    fashi.Items.Add(item);
                }
                dao.SaveOrUpdate(fashi);
                return Json(true);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }
    }
}